package com.lego.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lego.bean.Goods;
import com.lego.bean.Page;
import com.lego.bean.User;
import com.lego.service.UserService;
import com.lego.service.impl.UserServiceImpl;

@WebServlet("/CheckUsername")
public class CheckUsernameServlet extends HttpServlet {
	private UserService userService = new UserServiceImpl();
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		String userName = request.getParameter("userName");
		try {
			User user = userService.selectUserByname(userName);
			boolean jsonResponse;
			if(user != null) {
				jsonResponse = true;
			}else {
                jsonResponse = false; 
            }
			response.setContentType("application/json;charset=utf-8");
			ObjectMapper om = new ObjectMapper();
			String str = om.writeValueAsString(jsonResponse);
			response.getWriter().write(str);
		}catch (Exception e) {
			// TODO: handle exception
			String jsonResponse = "{\"error\":true, \"message\":\"检查用户名时发生错误\"}";
			out.print(jsonResponse);
		}
	}

}
